use std::sync::{Arc, Mutex};
use std::thread;

fn main() {
    let shared_vec = Arc::new(Mutex::new(Vec::new()));
    // 管理线程
    let mut handles = vec![];
    for i in 0..5 {
        let shared_vec = Arc::clone(&shared_vec);
        let handle = thread::spawn(move || {
            //获取锁
            let mut vec = shared_vec.lock().unwrap();
            // 修改Vec
            vec.push(i);
        });
        handles.push(handle);
    }

    //等待写入线程完成
    for handle in handles {
        handle.join().unwrap();
    }

    // 获取 Vec，并输出结果
    let final_vec = shared_vec.lock().unwrap();
    println!("Final Vec: {:?}", *final_vec);
}
